<?php
/*
Template Name: Accessi
*/
?>

<?php get_header(); ?>

<?php 
global $blogurl; 
global $rem_url, $rem_port, $rem_id;
?>




<script>

$(document).ready(function() {

$( "#data-a" ).datepicker("option", "dateFormat", "dd-mm-yy");


$(document).on('pagebeforeshow', '#nuovaPrestazione', function(){     
$('#qnt-p').val('')
$('#note-p').val('')
$("#prest-p").empty();
var myselect = $("#prest-p");
myselect[0].selectedIndex = 3;
myselect.selectmenu('refresh');

$("#piano-p").empty();
var myselect2 = $("#piano-p");
myselect2[0].selectedIndex = 3;
myselect2.selectmenu('refresh'); 
    //var url = 'http://195.128.234.70:8080/webservice/PianiServlet?idassistito='+assInfo.id;

    var url = '<?php echo get_remote_url($rem_url, $rem_port, "PianiServlet", $rem_id); ?>&idassistito=' + assInfo.id;          
    
    $.ajax({
        url: url ,
        dataType: "json",
        async: true,
        success: function (result) {
			
            ajax.parsePiani(result);
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });         
});

$(document).on('pagebeforeshow', '#accessi', function(){      
	var url = '<?php echo get_remote_url($rem_url, $rem_port, "GetAccServlet", $rem_id); ?>';
              
    
    $.ajax({
        url: url ,
        dataType: "json",
        async: true,
        success: function (result) {
			
            ajax.parseJSONA(result);
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });         
});

$(document).on('pagebeforeshow', '#listaPrest', function(){   
var idA=accessiInfo.id;
var ii=0;
var giusto=0;
while (ii<accessiInfo.result.length)
  {
  if (idA==accessiInfo.result[ii].id) giusto=ii;
   ii=ii+1;
  }
    //alert(giusto);
	//alert(accessiInfo.result[giusto].cognome+' '+accessiInfo.result[giusto].nome+ ' '+accessiInfo.result[giusto].dataN);
	 $('#assistito-am').val(accessiInfo.result[giusto].cognome+' '+accessiInfo.result[giusto].nome+ ' '+accessiInfo.result[giusto].dataN);
 $('#km-am').val(accessiInfo.result[giusto].km);
 $('#note-am').val(accessiInfo.result[giusto].note);
  $('#data-am').val(accessiInfo.result[giusto].data);

   //var url = 'http://195.128.234.70:8080/webservice/GetEffettuatiServletOperatori?accesso='+accessiInfo.id;

   var url = '<?php echo get_remote_url($rem_url, $rem_port, "GetEffettuatiServletOperatori", $rem_id); ?>&accesso=' + accessiInfo.id;              
    
    $.ajax({
        url: url ,
        dataType: "json",
        async: true,
        success: function (result) {
			
            ajax.parseJSONPrest(result);
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });         
});



$(document).on('pagebeforeshow', '#sceltaAss', function(){      
	var url = '<?php echo get_remote_url($rem_url, $rem_port, "WelcomeServlet", $rem_id); ?>';
              
    
    $.ajax({
        url: url ,
        dataType: "json",
        async: true,
        success: function (result) {
			
            ajax.parseAss(result);
			
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });             
});





$(document).on('vclick', '#nuovoButton', function(){  
$('#data-a').val('');
$('#assistito-a').val('');
$('#note-a').val('')
$('#km-a').val('')
$.mobile.changePage( "#nuovoAccesso", { transition: "slide", changeHash: false });
});

$(document).on('vclick', '#nuovaPrestButton', function(){  

$.mobile.changePage( "#nuovaPrestazione", { transition: "slide", changeHash: false });
});

$( "#assistito-a" ).focus(function() {
 
    $.mobile.changePage( "#sceltaAss", { transition: "slide", changeHash: false });
  
});
$(document).on('vclick', '#assistiti-list li a', function(){  

    assInfo.id = $(this).attr('data-id');
	$( "#assistito-a" ).val($(this).attr('data-nome'));
	$.mobile.changePage( "#nuovoAccesso", { transition: "slide", changeHash: false });
    
});

$(document).on('vclick', '#del', function(){  

    $( "#popupDialog" ).popup("open");
    
});

$(document).on('vclick', '#delp', function(){  
   

    $( "#popupDelp" ).popup("open");
	prestDettInfo.id = $(this).attr('data-id');
    
});

$(document).on('vclick', '#backPrest', function(){  
   
	$( "#popupDelp" ).popup({ history: false });
    $( "#popupDelp" ).popup("close");
    
});

$(document).on('vclick', '#okPrest', function(){  
$.ajax({
		//url: 'http://195.128.234.70:8080/webservice/DeleteEffettuatiServlet?id='+prestDettInfo.id,

    	url: '<?php echo get_remote_url($rem_url, $rem_port, "DeleteEffettuatiServlet", $rem_id); ?>&id=' + prestDettInfo.id,
        dataType: "json",
        async: true,
        success: function (result) {
			
		var url = '<?php echo get_remote_url($rem_url, $rem_port, "GetEffettuatiServletOperatori", $rem_id); ?>&accesso=' + accessiInfo.id;
           
    		
    $.ajax({
        url: url ,
        dataType: "json",
        async: true,
        success: function (result) {
			
            ajax.parseJSONPrest(result);
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });  
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });  
    
	$( "#popupDelp" ).popup({ history: false });
    $( "#popupDelp" ).popup("close");
    
});

$(document).on('vclick', '#okPrestNuovo', function(){ 
	$( "#popPrestDialog" ).popup({ history: false });
    $( "#popPrestDialog" ).popup("close");
	$.mobile.changePage( "#listaPrest", { transition: "slide", changeHash: false });
});

$(document).on('vclick', '#okAccMod', function(){ 
	$( "#popAccModDialog" ).popup({ history: false });
    $( "#popAccModDialog" ).popup("close");
	
});

$(document).on('vclick', '#more', function(){  

    accessiInfo.id = $(this).attr('data-id');
	assInfo.id=$(this).attr('data-id-ass');
	accessiData.id=$(this).attr('data-data');
	//$( "#assistito-a" ).val($(this).attr('data-nome'));
	//$.mobile.changePage( "#nuovaPrestazione", { transition: "slide", changeHash: false });
	$.mobile.changePage( "#listaPrest", { transition: "slide", changeHash: false });
    
});
 



$(document).on('vclick', '#submit-prest', function(){  
//&ora_operatore=24/02/2014%2018.16.40&tipologia=ora_sistema=24/02/2014%2018.16.40&ora_sistema_sql=2014-02-24
var sis=accessiData.id.split("-");

//var url = 'http://195.128.234.70:8080/webservice/EffettuatiServlet?id_assistito='+assInfo.id+'&id_operatore=5&id_prestazione='+$('#prest-p').val()+'&id_programma='+$('#piano-p').val()+'&note='+$('#note-p').val()+'&qnt='+$('#qnt-p').val()+'&id_accesso='+accessiInfo.id+'&id_piano='+dettPianoInfo.id+'&ora_sistema_sql='+accessiData.id+'&ora_sistema='+sis[2]+'/'+sis[1]+'/'+sis[0];             
var url = '<?php echo get_remote_url($rem_url, $rem_port, "EffettuatiServlet", $rem_id); ?>&id_assistito='+assInfo.id+'&id_prestazione='+$('#prest-p').val()+'&id_programma='+$('#piano-p').val()+'&note='+$('#note-p').val()+'&qnt='+$('#qnt-p').val()+'&id_accesso='+accessiInfo.id+'&id_piano='+dettPianoInfo.id+'&ora_sistema_sql='+accessiData.id+'&ora_sistema='+sis[2]+'/'+sis[1]+'/'+sis[0];

    $.ajax({
        url: url ,
        dataType: "json",
        async: true,
        success: function (result) {
			
  
		$( "#popPrestDialog" ).popup("open");
			
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });  
});

$(document).on('change', '#piano-p', function(){ 
  	//var url = 'http://195.128.234.70:8080/webservice/ProgrammiDettaglioServlet?idprogramma='+$('#piano-p').val();

  	var url = '<?php echo get_remote_url($rem_url, $rem_port, "ProgrammiDettaglioServlet", $rem_id); ?>&id_assistito=' + $('#piano-p').val();         
    
    $.ajax({
        url: url ,
        dataType: "json",
        async: true,
        success: function (result) {
			
            ajax.parseDettPiano(result);
			
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });     
});

$(document).on('vclick', '#submit-accesso', function(){  
	$('#data-a').val()
	var tempo=$('#data-a').val().split("/");
	//var url = 'http://195.128.234.70:8080/webservice/EffettuatiAccServlet?id_operatore=5&id_assistito='+assInfo.id+'&data='+tempo[2]+'-'+tempo[0]+'-'+tempo[1]+'&note='+$('#note-a').val()+'&km='+$('#km-a').val();

	var url = '<?php echo get_remote_url($rem_url, $rem_port, "EffettuatiAccServlet", $rem_id); ?>&id_assistito='+assInfo.id+'&data='+tempo[2]+'-'+tempo[0]+'-'+tempo[1]+'&note='+$('#note-a').val()+'&km='+$('#km-a').val();        
    
    $.ajax({
        url: url ,
        dataType: "json",
        async: true,
        success: function (result) {
			
           $( "#popupAccDialog" ).popup({
  afterclose: function( event, ui ) {
  event.preventDefault();}
});
		$( "#popupAccDialog" ).popup("open");
			
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });  
});

$(document).on('vclick', '#submit-accessom', function(){  
$('#data-am').val()
var tempo=$('#data-am').val().split("/");
//var url = 'http://195.128.234.70:8080/webservice/EffettuatiAccServlet?data='+tempo[2]+'-'+tempo[0]+'-'+tempo[1]+'&note='+$('#note-am').val()+'&km='+$('#km-am').val()+'&id_accesso='+accessiInfo.id;

var url = '<?php echo get_remote_url($rem_url, $rem_port, "EffettuatiAccServlet", $rem_id); ?>&data='+tempo[2]+'-'+tempo[0]+'-'+tempo[1]+'&note='+$('#note-am').val()+'&km='+$('#km-am').val()+'&id_accesso='+accessiInfo.id   

    
    $.ajax({
        url: url ,
        dataType: "json",
        async: true,
        success: function (result) {
			
           $( "#popAccModDialog" ).popup({
  afterclose: function( event, ui ) {
  event.preventDefault();}
});
		$( "#popAccModDialog" ).popup("open");
			
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });  
});

var accessiInfo = {
    id : null,
    result : null
}
var prestDettInfo = {
    id : null,
    result : null
}

var dettPianoInfo = {
    id : null,
    result : null
}

var prestazioniInfo = {
    id : null,
    result : null
}

var pianiInfo = {
    id : null,
    result : null
}

var accessiData = {
    id : null,
    result : null
}

var ajax = {  
    parseDettPiano:function(result){  
        dettPianoInfo.result = result.users;
		dettPianoInfo.id = result.users[0].idPiano;
		$('#prest-p').empty();
        $.each(result.users, function(i, row) {
           // console.log(JSON.stringify(row));
         $('#prest-p').append('<option value="'+row.mobile+'">'+row.prestazione+'  '+row.cadenza+'</option>');
		});
        var myselect = $("#prest-p");
myselect[0].selectedIndex = 3;
myselect.selectmenu('refresh');
    },
	
    parseJSONA:function(result){  
        accessiInfo.result = result.orders;
		$('#accessi-list').empty();
        $.each(result.orders, function(i, row) {
           // console.log(JSON.stringify(row));
            $('#accessi-list').append('<li><a href="" id="more" data-id="' + row.id + '" data-data="'+row.data+'" data-id-ass="' + row.id_assistito + '"><h3>' + row.cognome +' '+row.nome+ ' '+row.dataN+'</h3><p>' + row.data + '  Km:'+ row.km+'</p></a><a href="" id="del" data-rel="popup" data-position-to="window" data-transition="pop">Elimina accesso</a></li>');
        });
        $('#accessi-list').listview('refresh');
    },
	parseAss:function(result){  
        assInfo.result = result.users;
		assInfo.id = result.users[0].mobile;
        $.each(result.users, function(i, row) {
           // console.log(JSON.stringify(row));
            $('#assistiti-list').append('<li><a href="" data-id="' + row.mobile + '" data-nome="' + row.cognome +' '+row.nome+' '+ row.datan + '"><h3>' + row.cognome +' '+row.nome+ '</h3><p>' + row.datan + '</p></a></li>');
        });
        $('#assistiti-list').listview('refresh');
		
    } ,
	parsePiani:function(result){  
        pianiInfo.result = result.users;
		$('#piano-p').empty();
        $.each(result.users, function(i, row) {
           // console.log(JSON.stringify(row));
           // $('#prest-p').append('<li><a href="" data-id="' + row.mobile + '" data-nome="' + row.cognome +' '+row.nome+' '+ row.datan + '"><h3>' + row.cognome +' '+row.nome+ '</h3><p>' + row.datan + '</p></a></li>');
        $('#piano-p').append('<option value="'+row.mobile+'">'+row.protocolloDomanda+' scad.'+row.dataFine+'</option>');
		});
        //$('#prest-p').listview('refresh');
		
    },
	
	parseJSONPrest:function(result){  
        prestazioniInfo.result = result.orders;
		$('#prestazioni-list').empty();
        $.each(result.orders, function(i, row) {
           // console.log(JSON.stringify(row));
            $('#prestazioni-list').append('<li><a href="" id="morep" data-id="' + row.id_prestazioni_app + '" data-nome="' + row.who + '"><h3>' + row.message +'  (x'+row.qnt+')</h3><h2>' + row.kmessage + '</h2></a><a href="" data-id="' + row.id_prestazioni_app + '" id="delp" data-rel="popup" data-position-to="window" data-transition="pop">Elimina prestazione</a></li>');
        });
        $('#prestazioni-list').listview('refresh');
		
    }
}
});

var assInfo = {
    id : null,
    result : null
}


</script>




	<div role="main" class="ui-content jqm-content">

		<?php
		
		if (current_user_can('view_access')) {	
	    	if (have_posts()) {
	    		while (have_posts()) {
	    			the_post();	    		
	    			?>
	    			<h1><?php the_title(); ?></h1>
	    			
	    			<?php 
					if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { 
						the_post_thumbnail('full', array("class" => "img-thumbnail"));
					} 
					?>	
	    			
	    			<?php the_content(); ?>    			
		
						<input type="button" id="nuovoButton" value="Nuovo Accesso">
						<form class="ui-filterable">
						    <input id="filterBasic-input" data-type="search">
						</form>
				        
				        <ul data-role="listview" data-split-icon="delete" data-filter="true" data-input="#filterBasic-input" id="accessi-list" data-theme="a">
				        
				        </ul>
				        
				        <div data-role="popup" id="popupDialog" data-overlay-theme="b" data-theme="b" data-dismissible="false" style="max-width:400px;">
						    <div data-role="header" data-theme="a">
						    	<h1>Cancella</h1>
						    </div>
						    <div role="main" class="ui-content">
						        <h3>Cancellare accesso?</h3>
								<p>Le prestazioni collegate all'accesso saranno perdute.</p>
						    	<a href="index.html" data-rel="back" class="ui-shadow ui-btn ui-corner-all ui-btn-b ui-icon-check ui-btn-icon-left ui-btn-inline ui-mini">Ok</a>
						    	<a href="index.html" data-rel="back" class="ui-shadow ui-btn ui-corner-all ui-btn-inline ui-mini">Annulla</a>
						    </div>
						</div>
				        
				        
	    			
	    		<?php 
	    		}
	    	} 
		} else {		
			echo no_permissions(); 		
		}
    	
    	?>


	</div><!-- /content -->
	
<?php require_once ( get_template_directory() . '/menu.php' );?>
  
<?php get_sidebar(); ?>

<?php get_footer(); ?>



<?php if (current_user_can('view_access')) { ?>

	<div data-role="page" id="nuovoAccesso">
	    <div data-theme="a" data-role="header">
	        <a href="<?php echo $blogurl; ?>/accessi/#accessi" class="ui-btn-left" data-transition="slide" data-direction="reverse">Back</a>                        
	        <h3>
	            <div id="titoloAnagrafica">Nuovo Accesso</div>
	        </h3>
	    </div>        
	    <div data-role="content">
	        
	        <div class="ui-field-contain">
				<label for="data-a">Data:</label>
				<input type="text" name="data-a" id="data-a" placeholder="Data" data-role="date">
				
			</div>  
			<div class="ui-field-contain">
				<label for="assistito-a">Assistito:</label>
				<input type="text" name="assistito-a" id="assistito-a" placeholder="Assistito" value="">
			</div>  
			<div class="ui-field-contain">
				<label for="km-a">Km:</label>
				<input type="text" name="km-a" id="km-a" placeholder="Km" value="">
			</div> 
			<div class="ui-field-contain">
				<label for="note-a">Note:</label>
				<input type="text" name="note-a" id="note-a" placeholder="Note" value="">
			</div> 
			
			<button type="submit" id="submit-accesso" class="ui-shadow ui-btn ui-corner-all">Salva</button>
	        </ul>
			<div data-role="popup" id="popupDialog" data-overlay-theme="b" data-theme="b" data-dismissible="false" style="max-width:400px;">
			    <div data-role="header" data-theme="a">
			    <h1>Salvataggio avvenuto</h1>
			    </div>
			    <div role="main" class="ui-content">
			        <h3 class="ui-title">Le informazioni sono state aggiornate</h3>
			    
			        <a  class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-b" data-rel="back">OK</a>
			       
			    </div>
			</div>
	    </div>
		
	</div>    
	
	
	<div data-role="page" id="nuovaPrestazione">
	    <div data-theme="a" data-role="header">
	        <a href="../accessi/#listaPrest" class="ui-btn-left" data-transition="slide" data-direction="reverse">Back</a>                        
	        <h3>
	            <div id="titoloAnagrafica">Nuova Prestazione</div>
	        </h3>
	    </div>        
	    <div data-role="content">
	        
	        <div class="ui-field-contain">
				<label for="piano-p">Piano:</label>
				<select name="piano-p" id="piano-p">
					
				</select>
			</div>
			
			<div class="ui-field-contain">
				<label for="prest-p">Prestazione:</label>
				<select name="prest-p" id="prest-p">
					
				</select>
			</div>
			
			<div class="ui-field-contain">
				<label for="qnt-p">Quantita:</label>
				<input type="number" name="qnt-p" id="qnt-p" placeholder="Quantita" value="1">
			</div> 
			<div class="ui-field-contain">
				<label for="note-p">Note:</label>
				<input type="text" name="note-p" id="note-p" placeholder="Note" value="">
			</div> 
			
			<button type="submit" id="submit-prest" class="ui-shadow ui-btn ui-corner-all">Salva</button>
	        
			<div data-role="popup" id="popPrestDialog" data-overlay-theme="b" data-theme="b" data-dismissible="false" style="max-width:400px;">
			    <div data-role="header" data-theme="a">
			    <h1>Salvataggio avvenuto</h1>
			    </div>
			    <div role="main" class="ui-content">
			        <h3 class="ui-title">Le informazioni sono state aggiornate</h3>
			    
			        <a  class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-b" id="okPrestNuovo" href="">OK</a>
			       
			    </div>
			</div>
	    </div>
		
	</div> 
	
	
	
	<div data-role="page" id="sceltaAss" >
	    <div data-role="content">
	        <div style="padding:10px 20px;">
	           <form class="ui-filterable">
				    <input id="filterBasic-input-ass" data-type="search">
				</form>
	        <ul data-role="listview"  data-filter="true" data-input="#filterBasic-input-ass" id="assistiti-list" data-theme="a">
	        </div>
		</div>   
	</div>
	
	
	
	
	<div data-role="page" id="listaPrest" >
		<div data-theme="a" data-role="header">
			<a href="../accessi/#accessi" class="ui-btn-left" data-transition="slide" data-direction="reverse">Back</a>                        
		        <h3>
		            <div id="titoloAnagrafica">Prestazioni</div>
		        </h3>
		    </div>
		       
		    <div data-role="content">
				<input type="button" id="nuovaPrestButton" value="Nuova Prestazione">
			        <div style="padding:10px 20px;">
			           <form class="ui-filterable">
			    			<input id="filterBasic-input-prac" data-type="search">
						</form>
		        <ul data-role="listview" data-split-icon="delete" data-filter="true" data-input="#filterBasic-input-prac" id="prestazioni-list" data-theme="a">
				
				</ul>
			
			</div>
			<div class="ui-field-contain">
				<label for="data-am">Data:</label>
				<input type="text" name="data-am" id="data-am" placeholder="Data" data-role="date">
				
			</div>  
			<div class="ui-field-contain">
				<label for="assistito-am">Assistito:</label>
				<input type="text" name="assistito-am" id="assistito-am" placeholder="Assistito" value="">
			</div>  
			<div class="ui-field-contain">
				<label for="km-am">Km:</label>
				<input type="text" name="km-am" id="km-am" placeholder="Km" value="">
			</div> 
			<div class="ui-field-contain">
				<label for="note-am">Note:</label>
				<input type="text" name="note-am" id="note-am" placeholder="Note" value="">
			</div> 
				
			<button type="submit" id="submit-accessom" class="ui-shadow ui-btn ui-corner-all">Modifica</button>
			<div data-role="popup" id="popupDelp" data-overlay-theme="b" data-theme="b" data-dismissible="false" style="max-width:400px;">
			    <div data-role="header" data-theme="a">
			    <h1>Cancella</h1>
			    </div>
		    	<div role="main" class="ui-content">
		        	<h3>Cancellare prestazione?</h3>
					<p>Le informazioni contenute nella prestazione saranno perdute.</p>
				    <a href="" id="okPrest" class="ui-shadow ui-btn ui-corner-all ui-btn-b ui-icon-check ui-btn-icon-left ui-btn-inline ui-mini">Ok</a>
				    <a href="" id="backPrest" class="ui-shadow ui-btn ui-corner-all ui-btn-inline ui-mini">Annulla</a>
		       
		    	</div>
			</div>
			
			<div data-role="popup" id="popAccModDialog" data-overlay-theme="b" data-theme="b" data-dismissible="false" style="max-width:400px;">
		    	<div data-role="header" data-theme="a">
		    		<h1>Salvataggio avvenuto</h1>
		    	</div>
		    	<div role="main" class="ui-content">
		        	<h3 class="ui-title">Le informazioni sono state aggiornate</h3>		    
		        	<a  class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-btn-b" id="okAccMod" href="">OK</a>		       
		    	</div>
			</div>
		</div>
	   
	</div>
	
<?php 
} else {		
	echo no_permissions(); 		
}
?>